package cn.yujian95.hospital.service.impl;

import cn.yujian95.hospital.common.api.CommonResult;
import cn.yujian95.hospital.dto.HospitalDoctorDTO;
import cn.yujian95.hospital.dto.param.HospitalDoctorParam;
import cn.yujian95.hospital.dto.update.DoctorInfo;
import cn.yujian95.hospital.entity.HospitalDoctor;
import cn.yujian95.hospital.entity.HospitalDoctorExample;
import cn.yujian95.hospital.entity.HospitalSpecial;
import cn.yujian95.hospital.mapper.HospitalDoctorMapper;
import cn.yujian95.hospital.mapper.HospitalSpecialRelationMapper;
import cn.yujian95.hospital.service.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author YuJian95  clj9509@163.com
 * @date 2020/2/4
 */
@Service
public class HospitalDoctorInfoServiceImpl implements IHospitalDoctorService {

    @Resource
    private HospitalDoctorMapper doctorInfoMapper;

    @Resource
    private IHospitalSpecialService specialService;

    @Resource
    private IHospitalOutpatientService outpatientService;

    @Resource
    private IHospitalInfoService hospitalInfoService;

    @Resource
    private HospitalSpecialRelationMapper hospitalSpecialRelationMapper;

    @Resource
    private IVisitPlanService visitPlanService;


    /**
     * 添加医生信息
     *
     * @param param 医生信息参数
     * @return 是否成功
     */
    @Override
    public boolean insert(HospitalDoctorParam param) {
        HospitalDoctor info = new HospitalDoctor();

        BeanUtils.copyProperties(param, info);

        info.setGmtCreate(new Date());
        info.setGmtModified(new Date());

        return doctorInfoMapper.insertSelective(info) > 0;
    }

    /**
     * 更新医生信息
     *
     * @param id    医生编号
     * @param param 医生信息参数
     * @return 是否成功
     */
    @Override
    public boolean update(Long id, HospitalDoctorParam param) {
        HospitalDoctor info = new HospitalDoctor();

        BeanUtils.copyProperties(param, info);

        info.setId(id);
        info.setGmtModified(new Date());

        return doctorInfoMapper.updateByPrimaryKeySelective(info) > 0;
    }

    /**
     * 是否存在医生信息
     *
     * @param id 医生编号
     * @return 是否存在
     */
    @Override
    public boolean count(Long id) {
        HospitalDoctorExample example = new HospitalDoctorExample();

        example.createCriteria()
                .andIdEqualTo(id);

        return doctorInfoMapper.countByExample(example) > 0;
    }

    /**
     * 获取转换后的对象信息
     *
     * @param id 医生编号
     * @return 转换后的对象
     */
    @Override
    public Optional<HospitalDoctorDTO> getConvert(Long id) {
        return Optional.ofNullable(convert(doctorInfoMapper.selectByPrimaryKey(id)));
    }

    /**
     * 获取医生名称
     *
     * @param id 医生编号
     * @return 医生名称，空则，返回未知
     */
    @Override
    public String getName(Long id) {
        return getOptional(id).map(HospitalDoctor::getName).orElse("未知");
    }

    /**
     * 获取医生信息
     *
     * @param id 医生编号
     * @return 医生编号
     */
    @Override
    public Optional<HospitalDoctor> getOptional(Long id) {
        return Optional.ofNullable(doctorInfoMapper.selectByPrimaryKey(id));
    }

    /**
     * 删除医生信息
     *
     * @param id 医生编号
     * @return 是否成功
     */
    @Override
    public boolean delete(Long id) {
        return doctorInfoMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 查找医生信息
     *
     * @param name     医生名称
     * @param pageNum  第几页
     * @param pageSize 页大小
     * @return 医生信息列表
     */
    @Override
    public List<HospitalDoctorDTO> list(String name, Integer pageNum, Integer pageSize, Long hospitalId) {

        PageHelper.startPage(pageNum, pageSize);

        HospitalDoctorExample example = new HospitalDoctorExample();

        if (!StringUtils.isEmpty(name)) {
            example.createCriteria()
                    .andNameLike("%" + name + "%");
        }

        List<HospitalDoctor> hospitalDoctors = doctorInfoMapper.selectByExample(example);
        // 删选是否为这个学校id
        if(hospitalId != null) {

            // filter就是表示不要的
           hospitalDoctors = hospitalDoctors.stream().filter((item) -> {
               System.out.println(item.getHospitalId());
                return item.getHospitalId() != hospitalId;
            }).collect(Collectors.toList());
        }

        return hospitalDoctors.stream()
                .map(this::convert)
                .collect(Collectors.toList());
    }

    /**
     * 查找医生信息列表
     *
     * @param name         医生名称
     * @param specialId    专科编号
     * @param outpatientId 门诊编号
     * @param pageNum      第几页
     * @param pageSize     页大小
     * @return 医生信息列表
     */
    @Override
    public List<HospitalDoctorDTO> list(String name, Long specialId, Long outpatientId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        HospitalDoctorExample example = new HospitalDoctorExample();

        HospitalDoctorExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (specialId != null) {
            criteria.andSpecialIdEqualTo(specialId);
        }

        if (outpatientId != null) {
            criteria.andOutpatientIdEqualTo(outpatientId);
        }

        return doctorInfoMapper.selectByExample(example).stream()
                .map(this::convert)
                .collect(Collectors.toList());
    }

    @Override
    public List<HospitalDoctorDTO> getDoctorByHospitalId(Long hospitalId, String week, String days) {
        /*List<HospitalSpecial> list = specialService.list(hospitalId, 1, 100);

        // 获取了所有的special
        List<Long> specialIds = list.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());*/

        // 现在不用获取科室id 直接根据hospitalId来实现就可以了

        // 根据specailId来获取所有医生的信息
        List<HospitalDoctorDTO> doctorDTOS = doctorInfoMapper.getInfoByHospitalId(hospitalId);

        // 将收集出来的来看看
        List<HospitalDoctorDTO> collect = doctorDTOS.stream().filter((item) -> {
            String day = item.getDay();
            // 还要看今天是否是属于这个病假的
            String[] split = day.split(",");
            List<String> list1 = Arrays.asList(split);
            return list1.contains(week);
        }).collect(Collectors.toList());

        if(collect != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            // 今天的月日
            Date date = new Date(); // 2022-21-23
            // 找不包含的

//            System.out.println("format:" + format);
            List<HospitalDoctorDTO> doctorDTOS1 = collect.stream().filter((item) -> {
                String jia = item.getJia();
                if(StringUtils.isEmpty(jia)) {
                    return true;
                } else {
                    String[] split = jia.split(",");
                    // 里面的就是年-月-日
                    List<String> list1 = Arrays.asList(split);
                    return !list1.contains(days);
                }
            }).collect(Collectors.toList());
            return doctorDTOS1;
        }



        return collect;




    }

    /**
     * 转换医生信息
     * 增加专科名称，门诊名称
     *
     * @param doctor 医生信息
     * @return 医生信息封装对象
     */
    private HospitalDoctorDTO convert(HospitalDoctor doctor) {


        HospitalDoctorDTO dto = new HospitalDoctorDTO();


        BeanUtils.copyProperties(doctor, dto);

        // 设置专科名称
        dto.setSpecialName(specialService.getName(doctor.getSpecialId()));

        // 设置门诊名称
        dto.setOutpatientName(outpatientService.getName(doctor.getOutpatientId()));

        // 设置医院名称
        dto.setHospitalName(hospitalInfoService.getName(doctor.getHospitalId()));

        // 设置时间段
        dto.setDuan(visitPlanService.getDuan(dto.getId()));
        // 设置day
        dto.setDay(visitPlanService.getDay(dto.getId()));
        // 设置jia
        dto.setJia(visitPlanService.getJia(dto.getId()));

        return dto;
    }
}
